package cn.szabadsag.pet.service.impl;

import cn.szabadsag.base.service.impl.BaseServiceImpl;
import cn.szabadsag.org.domain.Employee;
import cn.szabadsag.org.domain.Shop;
import cn.szabadsag.org.mapper.ShopMapper;
import cn.szabadsag.pet.domain.SearchMasterMsg;
import cn.szabadsag.pet.mapper.SearchMasterMsgMapper;
import cn.szabadsag.pet.query.SearchMasterMsgQuery;
import cn.szabadsag.pet.service.ISearchMasterMsgService;
import cn.szabadsag.user.domain.User;
import cn.szabadsag.utils.DistanceUtil;
import cn.szabadsag.utils.LoginContext;
import cn.szabadsag.utils.Point;
import cn.szabadsag.utils.QueryResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {


    @Autowired
    private ShopMapper shopMapper;


    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;


    /**
     * 发布接口
     * @param searchMasterMsg
     */
    @Override
    @Transactional
    public void publish(SearchMasterMsg searchMasterMsg, HttpServletRequest request) {
        //  1.设置状态为待审核 保存状态---0表示待激活/待审核
        searchMasterMsg.setState(0);
        //  2.获取登录账户
        User user = (User) LoginContext.getCurrentUser(request);
        //  调用工具类设置user_id
        searchMasterMsg.setUser_id(user.getId());


        //  使用工具类获取离用户最近的商家
        Point point = DistanceUtil.getPoint(searchMasterMsg.getAddress());
        //List<Shop> shops = shopMapper.loadAll();
        //System.out.println("所有商店：" + shops);
        //  查询相同城市的宠物店
        List<Shop> shops = shopMapper.loadByCity(searchMasterMsg.getCity());
        System.out.println("相同城市的所有商店：" + shops);
        Shop nearestShop = DistanceUtil.getNearestShop(point, shops);

        //  设置店铺id
        searchMasterMsg.setShop_id(nearestShop.getId());

        //  保存数据
        super.save(searchMasterMsg);
    }

    /**
     * 高级查询
     * @param searchMasterMsgQuery
     * @param request
     * @return
     */
    @Override
    public QueryResult<SearchMasterMsg> queryPage(SearchMasterMsgQuery searchMasterMsgQuery, HttpServletRequest request) {
        //  获取登录用户信息
        Employee employee = (Employee)LoginContext.getCurrentUser(request);
        System.out.println("登录用户的信息：" + employee);
        /*
        登录用户的信息：Employee(username=15228553115,
                     email=15228553115@163.com,
                     phone=15228553115,
                     salt=dc17523a-595b-4b4a-b689-e2da774f82ab,
                     password=467d8e037776d042625b35bb3f8c17d3,
                     age=null,
                     state=0,
                     logininfo_id=331,
                     shop_id=27)
         */
        if(employee.getShop_id() == null){//  如果是管理员登录
            //  管理员可以查看所有商铺的寻主信息
            return super.queryPage(searchMasterMsgQuery);
        }else {//  如果是普通商铺用户登录
            //  获取分页总条数
            Long count = searchMasterMsgMapper.queryCountByShopId(employee.getShop_id());
            //  获取分页数据
            //  商铺用户只能查看自己商铺对应的寻主信息
            searchMasterMsgQuery.setShop_id(employee.getShop_id());
            List<SearchMasterMsg> data = searchMasterMsgMapper.loadByShopId(searchMasterMsgQuery);
            return new QueryResult<>(count,data);
        }
    }
}
